package huajiu.com.serviceImpl;

import huajiu.com.base.JsonUtils;
import huajiu.com.base.RecordResult;
import huajiu.com.base.ResultEntity;
import huajiu.com.dao.ConsumerHistoricaldataDao;
import huajiu.com.domain.ConsumerHistoricaldata;
import huajiu.com.service.ConsumerHistoricaldataService;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ConsumerHistoricaldataServiceImpl implements ConsumerHistoricaldataService{
	
	@Autowired
	private ConsumerHistoricaldataDao comsunmerHistoricaldataDao;

	@Override
	public ResultEntity selectConsumerHistoricaldataById(String id) {
		ResultEntity entity = new ResultEntity();
		entity.setErrorMessage("error");
		ConsumerHistoricaldata selectByPrimaryKey = comsunmerHistoricaldataDao.getConsumerHistoricaldata(id);
		if (selectByPrimaryKey != null && selectByPrimaryKey.getPay()==1) {
			String detail = selectByPrimaryKey.getDetail();
			RecordResult jsonToObject = (RecordResult) JsonUtils.jsonToObject(detail, RecordResult.class);
			Map<String, Integer> cmap = jsonToObject.getCmap();
			Map<String, Integer> emap = jsonToObject.getEmap();
			Map<String,Integer> Newcamp = new HashMap<String, Integer>();
			Map<String,Integer> Newemap = new HashMap<String, Integer>();
			for (Map.Entry<String,Integer> entry:cmap.entrySet()) {
				if(entry.getValue()!=0){
					Newcamp.put(entry.getKey(), entry.getValue());
				}
			}
			for (Map.Entry<String,Integer> entry:emap.entrySet()) {
				if(entry.getValue()!=0){
					Newemap.put(entry.getKey(), entry.getValue());
				}
			}
			jsonToObject.setCmap(Newcamp);
			jsonToObject.setEmap(Newemap);
			jsonToObject.setCorporeity(selectByPrimaryKey.getCorporeity());
			entity.setData(jsonToObject);
			entity.setErrorMessage("success");
			return entity;
		}else if(selectByPrimaryKey != null && selectByPrimaryKey.getPay()==2){
			entity.setErrorMessage("not pay");
			return entity;
		}
		return entity;
	}

}
